package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.ProductConverter;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**********************
 *
 *@Author 闫星忠
 *@Time 2021/12/16  10:12
 *
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;


//    物资资料分页列表展示
    @Override
    public PageVO<ProductVO> findProductList(Integer pageNum, Integer pageSize,String categoryKeys, ProductVO productVO) {
        PageHelper.startPage(pageNum, pageSize);
        Example o = new Example(Product.class);
        Example.Criteria criteria=o.createCriteria();
        if (!StringUtils.isEmpty(categoryKeys)) {
            String[] strings = categoryKeys.split(",");
            switch (strings.length){
                case 3:
                    criteria.andEqualTo("threeCategoryId",strings[2]);
                    break ;
                case 2:
                    criteria.andEqualTo("twoCategoryId",strings[1]);
                    break ;
                case 1:
                    criteria.andEqualTo("oneCategoryId",strings[0]);
                    break ;
            }
        }
        if (!StringUtils.isEmpty(productVO.getName())) {
            criteria.andLike("name", "%" + productVO.getName() + "%");
        }
        if (!StringUtils.isEmpty(productVO.getStatus())) {
            criteria.andEqualTo("status", productVO.getStatus());
        }
        List<Product> products = productMapper.selectByExample(o);
        //转vo
        List<ProductVO> productVOS = ProductConverter.converterToProductVOList(products);
        PageInfo<Product> info = new PageInfo<>(products);
        return new PageVO<>(info.getTotal(), productVOS);
    }

//    添加物资资料
    @Override
    public void add(ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        product.setCreateTime(new Date());
        product.setModifiedTime(new Date());
        product.setStatus(2);
        @NotNull(message = "分类不能为空") Long[] categoryKeys = productVO.getCategoryKeys();
        product.setOneCategoryId(categoryKeys[0]);
        product.setTwoCategoryId(categoryKeys[1]);
        product.setThreeCategoryId(categoryKeys[2]);
//        35dcdf38-410c-450d-916d-7b7dca8e
        String pNum = UUID.randomUUID().toString();
        product.setPNum(pNum);
        productMapper.insert(product);

    }


//    编辑页，数据回显
    @Override
    public ProductVO edit(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product,productVO);
        Long[] categoryKeys = new Long[3];
        categoryKeys[0]=product.getOneCategoryId();
        categoryKeys[1]=product.getTwoCategoryId();
        categoryKeys[2]=product.getThreeCategoryId();
        productVO.setCategoryKeys(categoryKeys);

        return productVO;
    }
//    编辑页，数据提交，修改物资资料
    @Override
    public void update(Long id, ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        product.setModifiedTime(new Date());
        @NotNull(message = "分类不能为空") Long[] categoryKeys = productVO.getCategoryKeys();
        product.setOneCategoryId(categoryKeys[0]);
        product.setTwoCategoryId(categoryKeys[1]);
        product.setThreeCategoryId(categoryKeys[2]);
        product.setId(id);

        productMapper.updateByPrimaryKey(product);
    }

//    移入回收站
    @Override
    public void remove(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(1);
        productMapper.updateByPrimaryKey(product);
    }
//    移出回收站到待审核
    @Override
    public void back(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(0);
        productMapper.updateByPrimaryKey(product);
    }

//    从回收站删除物资资料
    @Override
    public void delete(Long id) {
        productMapper.deleteByPrimaryKey(id);
    }
//    待审核转到正常
    @Override
    public void publish(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(0);
        productMapper.updateByPrimaryKey(product);
    }


}
